/*******************************************************************
This file is part of iDialer.

iDialer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iDialer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iDialer.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

#pragma once
#include "Macros.h"

//----------------------------------------------------------------------
// Generic defines and data types
//

struct decodeUINT {                             // Structure associates
    UINT Code;                                  // messages 
                                                // with a function. 
    LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
}; 

enum SERVICETYPE {
    Phone, CallingCard, GoogleVoice, JaJah, Command
};

struct SERVICE {
    SERVICETYPE type;
    TCHAR title[64];
    TCHAR data[128];
};

#define SZ_APP_NAME                 TEXT("iDialer")
#define SZ_IDIALER_REG_KEY          TEXT("Software\\Supware.net\\iDialer")

// registry settings
#define DIALPLAN                    TEXT("dialplan")
#define DIALPLAN_DEFAULT            TEXT("+1_NNN_NNN_NNNN;1_(NNN)_NNN-NNNN;NNN-NNNN;(NNN)_NNN-NNNN")
#define SKIN                        TEXT("skin")
#define SKIN_DEFAULT                TEXT("default")
#define SERVICE_NUM                 TEXT("service")
#define SERVICE_NUM_DEFAULT         TEXT("0")
#define SERVICE_TYPE_FORMAT         TEXT("service%dtype")
#define SERVICE_TYPE_PHONE          TEXT("phone")
#define SERVICE_TYPE_CALLINGCARD    TEXT("callingcard")
#define SERVICE_TYPE_GOOGLEVOICE    TEXT("googlevoice")
#define SERVICE_TYPE_JAJAH          TEXT("jajah")
#define SERVICE_TYPE_COMMAND		TEXT("command")
#define SERVICE_TITLE_FORMAT        TEXT("service%dtitle")
#define SERVICE_DATA_FORMAT         TEXT("service%ddata")
#define LAST_DIALED_NUMBER          TEXT("lastDialedNumber")
#define EXIT_ON_MINIMIZE            TEXT("exitOnMinimize")
#define EXIT_ON_MINIMIZE_DEFAULT    TEXT("1")
#define SHOW_FULL_SCREEN			TEXT("showFullScreen")
#define SHOW_FULL_SCREEN_DEFAULT    TEXT("1")
#define VOLUME_KEYPAD				TEXT("volumeKeypad")
#define VOLUME_KEYPAD_DEFAULT		TEXT("13")
#define MY_NUMBER                   TEXT("myNumber")
#define COUNTRY_CODE                TEXT("countryCode")
#define COUNTRY_CODE_DEFAULT        TEXT("1")
#define INTL_PREFIX                 TEXT("intlPrefix")
#define INTL_PREFIX_DEFAULT         TEXT("011")

// command line arguments
#define COMMAND_LINE_CHOOSECONTACT  TEXT("-choosecontact")
#define COMMAND_LINE_NAME           TEXT("-name=")
#define ICONTACT_FAVORITES          TEXT("-favorites")
#define ICONTACT_RECENTS            TEXT("-recents")
#define ICONTACT_CONTACTS           TEXT("-contacts")
#define ICONTACT_SEARCH             TEXT("-search")

// parameters for WM_COMMAND
#define CMD_GOTO_FAVORITES          0x01
#define CMD_GOTO_RECENTS            0x02
#define CMD_GOTO_CONTACTS           0x03
#define CMD_GOTO_DIALER             0x04
#define CMD_GOTO_SEARCH             0x05
#define CMD_DIAL_DIGIT              0x20
#define CMD_CLEAR_DIGITS            0x21
#define CMD_PLACE_CALL              0x22
#define CMD_RELOAD_SETTINGS         0x23
#define CMD_DIAL_VOICEMAIL          0x24
#define CMD_SPEED_DIAL				0x25
#define CMD_PLACE_CALL_COMPLETE     0x26
#define CMD_CHANGE_SERVICE          0x30
#define CMD_CHANGE_SERVICE_TO		0x31
#define CMD_CUT						0x40
#define CMD_COPY					0x41
#define CMD_PASTE					0x42
#define CMD_GREEN_BUTTON            0x100
#define CMD_RED_BUTTON              0x101

// timers
#define IDT_TIMER_LONG_PRESS        10016

// These sizes are for screens of 96 DPI only
// For 192 DPI, these will be *2'ed
#define DEFAULT_DPI                 96
#define DEFAULT_SCREEN_WIDTH		240
#define DEFAULT_SKIN_HEIGHT			121

#define SKIN_MENU_BAR_HEIGHT        40
#define SKIN_MENU_BAR_WIDTH         240
#define SKIN_MENU_BAR_Y_OFFSET      40
#define SKIN_MENU_BAR_SEL_Y_OFFSET  80
#define SKIN_MENU_BAR_ICON_WIDTH    48
#define SKIN_ACTION_ICONS_Y_OFFSET  16
#define SKIN_ACTION_ICON_WIDTH      80
#define SKIN_ACTION_ICON_HEIGHT     24
#define MENU_BAR_ICON_WIDTH         48

#define NUMBER_FONT_SIZE            32
#define DIGIT_FONT_SIZE             24
#define CAPTION_FONT_SIZE           12
#define TITLEBAR_FONT_SIZE          13

#define SKIN_COLORS_Y_OFFSET			120
#define SKIN_COLOR_BACKGROUND			0   // 000000
#define SKIN_COLOR_TEXT					1	// FFFFFF
#define SKIN_COLOR_TEXT_DIM				2	// 808080
#define SKIN_COLOR_HIGHLIGHT			3	// 4b5aff
#define SKIN_COLOR_NUMBER_REGION_RGB1	4	// 687c96
#define SKIN_COLOR_NUMBER_REGION_RGB2	5	// 264269
#define SKIN_COLOR_NUMBER_REGION_RGB3	6	// 112e5a
#define SKIN_COLOR_NUMBER_REGION_RGB4	7	// 0a2851
#define SKIN_COLOR_BUTTON_HIGHLIGHT		8	// 333942
#define SKIN_COLOR_BUTTON_SHADOW		9	// 12161f
#define SKIN_COLOR_DIGIT_BUTTON_RGB1	10	// 1c232d
#define SKIN_COLOR_DIGIT_BUTTON_RGB2	11	// 0e1420
#define SKIN_COLOR_CALL_BUTTON_RGB1		12	// 9bce96
#define SKIN_COLOR_CALL_BUTTON_RGB2		13	// 40b33c
#define SKIN_COLOR_CALL_BUTTON_RGB3		14	// 1fa119
#define SKIN_COLOR_CALL_BUTTON_RGB4		15	// 27ab1e
#define SKIN_COLOR_ADD_BUTTON_RGB1		16	// 203047
#define SKIN_COLOR_ADD_BUTTON_RGB2		17	// 0b1c36
#define SKIN_COLOR_ADD_BUTTON_RGB3		18	// 041831
#define SKIN_COLOR_ADD_BUTTON_RGB4		19	// 041832
#define SKIN_COLOR_BACK_BUTTON_RGB1		20	// 203047
#define SKIN_COLOR_BACK_BUTTON_RGB2		21	// 0b1c36
#define SKIN_COLOR_BACK_BUTTON_RGB3		22	// 041831
#define SKIN_COLOR_BACK_BUTTON_RGB4		23	// 041832

// Language text
#define TEXT_CALL					TEXT("Call")
#define TEXT_VOICEMAIL              TEXT("Voicemail")
#define TEXT_CALL_USING             TEXT("Call using")
#define	TEXT_ASK					TEXT("Ask")

#define MAX_DIALPLAN                1024
#define MAX_COUNTRY_CODE            8
#define MAX_LOADSTRING              100
#define MAX_DIALSTRING              TAPIMAXDESTADDRESSSIZE 

#define VALID_DIGITS                TEXT("0123456789+#*pwPW")

//----------------------------------------------------------------------
// Function prototypes
//
HWND InitInstance(HINSTANCE, LPWSTR, int);
int TermInstance(HINSTANCE, int);

// Window procedures
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Message handlers
LRESULT DoActivate(HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommand(HWND, UINT, WPARAM, LPARAM);
LRESULT DoContextMenu(HWND, UINT, WPARAM, LPARAM);
LRESULT DoCopyData(HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain(HWND, UINT, WPARAM, LPARAM);
LRESULT DoKeyDown(HWND, UINT, WPARAM, LPARAM);
LRESULT DoKeyUp(HWND, UINT, WPARAM, LPARAM);
LRESULT DoLButtonDown(HWND, UINT, WPARAM, LPARAM);
LRESULT DoLButtonUp(HWND, UINT, WPARAM, LPARAM);
LRESULT DoMouseMove(HWND, UINT, WPARAM, LPARAM);
LRESULT DoNotify(HWND, UINT, WPARAM, LPARAM);
LRESULT DoPaintMain(HWND, UINT, WPARAM, LPARAM);
LRESULT DoSize(HWND, UINT, WPARAM, LPARAM);
LRESULT DoTimer(HWND, UINT, WPARAM, LPARAM);
LRESULT DoTitlebarCallback (HWND, UINT, WPARAM, LPARAM);
LRESULT DoRegistryCallback (HWND, UINT, WPARAM, LPARAM);

// Paint functions
void InitSurface(HWND);
COLORREF GetSkinRGB(int index);
void DrawContentOn(HDC hdc);
void DrawNumberRegion(HDC hdc, RECT rect);
void DrawDigitButton(HDC hdc, RECT rect, TCHAR digit, TCHAR * label);
void DrawCallButton(HDC hdc, RECT rect);
void DrawAddButton(HDC hdc, RECT rect);
void DrawBackButton(HDC hdc, RECT rect);
void DrawGlassySurface(HDC hdc, RECT rect, UINT index1, UINT index2,
					   UINT index3, UINT index4);

void CalculateClickRegion(POINT p);
void CalculateClickRegion(TCHAR key);

// Data handling functions
TCHAR GetDigitFor(POINT pt);
TCHAR GetDigitFor(int key);
void StripNonDigits(TCHAR * to, const TCHAR * from, int cchTo);
void ScrubPhoneNumber(TCHAR * szNumber, int cchNumber);
void FillDialString(void);
void AddContact(void);
void LoadSettings();
int ParseCommandLine(const TCHAR * tcszCmdLine);
void SplitString(TCHAR * pszOutput, size_t cchOutput, const TCHAR * pszInput, 
                 TCHAR sep, TCHAR ** s1, TCHAR ** s2, TCHAR ** s3 = 0);
void ExpandString(TCHAR * pszOutput, size_t cchOutput, 
                 const TCHAR * pszSearch, const TCHAR * pszSubstitute);

// Dialing functions
HRESULT Dial(const TCHAR * szNumber, const TCHAR * szName, int which);
HRESULT DialStandard(const TCHAR * szNumber, const TCHAR * szName);
HRESULT DialCallingCard(const TCHAR * szNumber, const TCHAR * szName, SERVICE * pService);
HRESULT DialGoogleVoice(const TCHAR * szNumber, const TCHAR * szName, SERVICE * pService);
HRESULT DialJaJah(const TCHAR * szNumber, const TCHAR * szName, SERVICE * pService);
HRESULT DialCommand(const TCHAR * szNumber, const TCHAR * szName, SERVICE * pService);